#include <iostream>
#include "SocketThread.h"

using namespace std;

int main()
{
    std::string socketType("");
    std::string localAddr("");
    std::string localPort("");
    std::string remoteAddr("");
    std::string remotePort("");
    std::string temp("");
    TcpServerSocket* server;
    TcpSocket* client;
    cout << "Socket type(tcpserver or tcp or udp):";
    cin >> socketType;
    if(socketType == "s")
    {
        //cout << "Local Addr:";
        //cin >> localAddr;
        localAddr = "192.168.1.105";
        //cout << "Local Port:";
        //cin >> localPort;
        localPort = "23333";
        cout << "Create TCP Server..." << endl;
        server = new TcpServerSocket(localAddr, atoi(localPort.c_str()), 10);
        server->start();
        while(1) {
            cout << "Input G to get received data, or Q to Quit";
            cin >> temp;
            if(temp[0] == 'g' || temp[0] == 'G')
            {
                std::map<std::string, TcpSocket*>::const_iterator itam = server->getClientMap()->begin();
                std::map<std::string, TcpSocket*>::const_iterator itamend = server->getClientMap()->end();
                for(; itam != itamend; ++itam)
                    cout << "client " << itam->second->getRemoteAddr() << ":" << itam->second->getRemotePort() << " " << itam->second->getDataFromBuffer() << endl;
            }
            else if(temp[0] == 'q' || temp[0] == 'Q')
            {
                delete server;
                break;
            }
        }
    }
    else if(socketType == "t")
    {
        //cout << "Local Addr:";
        //cin >> localAddr;
        localAddr = "192.168.1.105";
        //cout << "Local Port:";
        //cin >> localPort;
        localPort = "23334";
        //cout << "Remote Addr:";
        //cin >> remoteAddr;
        remoteAddr = "192.168.1.105";
        //cout << "Remote Port:";
        //cin >> remotePort;
        remotePort = "23333";
        cout << "Create TCP Client..." << endl;
        client = new TcpSocket(localAddr, atoi(localPort.c_str()), remoteAddr, atoi(remotePort.c_str()));
        client->start();
        while(1) {
            cout << "Input data and send to server, or Q to Quit";
            cin >> temp;
            if(temp[0] == 'q' || temp[0] == 'Q')
            {
                delete client;
                break;
            }
            else
            {
                client->sendData(temp.c_str());
            }
        }
    }
    else if(socketType == "udp")
    {
        cout << "Local Addr:";
        cin >> localAddr;
        cout << "Local Port:";
        cin >> localPort;
        cout << "Remote Addr:";
        cin >> remoteAddr;
        cout << "Remote Port:";
        cin >> remotePort;
    }
    else
    {
        cout << "Socket type Error" << endl;
        return 0;
    }

    cout << "SocketInfo: " << socketType << localAddr << localPort << remoteAddr << remotePort << endl;
    return 0;
}
